/*
 * HomePage.java
 *
 * Created on December 3, 2011, 10:21 AM
 */
package com.myapp.wicket;

import com.wicket.jquery.integration.autocomplete.JQueryAutoCompleteTextField;
import java.util.ArrayList;
import java.util.List;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.PropertyModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HomePage extends WebPage {

    private final Form<HomePage> form1;
    private final TextField<String> stateTextField;
    private String state = "";

    /*
     * A State class jsonified into an array and returned to the client
     */
    static class State {

        State() {
        }

        State(String name) {
            this.name = name;
        }
        String name;
    }

    private static class StatesDb {

        private static final String[] states = new String[]{
            "Alabama",
            "Alaska",
            "Arizona",
            "Arkansas",
            "California",
            "Colorado",
            "Connecticut",
            "Delaware",
            "Florida",
            "Georgia",
            "Hawaii",
            "Idaho",
            "Illinois",
            "Indiana",
            "Iowa",
            "Kansas",
            "Kentucky",
            "Louisiana",
            "Maine",
            "Maryland",
            "Massachusetts",
            "Michigan",
            "Minnesota",
            "Mississippi",
            "Missouri",
            "Montana",
            "Nebraska",
            "Nevada",
            "New Hampshire",
            "New Jersey",
            "New Mexico",
            "New York",
            "North Carolina",
            "North Dakota",
            "Ohio",
            "Oklahoma",
            "Oregon",
            "Pennsylvania",
            "Rhode Island",
            "South Carolina",
            "South Dakota",
            "Tennessee",
            "Texas",
            "Utah",
            "Vermont",
            "Virginia",
            "Washington",
            "West Virginia",
            "Wisconsin",
            "Wyoming"
        };

        static List<State> getStatesLike(String target) {

            List<State> matches = new ArrayList<State>();
            for (String s : states) {
                if (s.toLowerCase().startsWith(target.toLowerCase())) {
                    State state = new State(s);
                    matches.add(state);
                }
            }
            return matches;
        }
        
    }

    public HomePage() {    /*
         * A private static class for states which supports
         * querying for states that are like target and 
         * returning those in a list.
         */

        add(form1 = new Form<HomePage>("form1") {

            @Override
            protected void onSubmit() {
                super.onSubmit();
                Logger logger = LoggerFactory.getLogger(this.getClass());
                logger.info("The user selected: " + state);
            }
        });

        /*
         * A TextField with JQueryAutocompleteBehavior
         */
        form1.add(stateTextField = new JQueryAutoCompleteTextField<String>("state", 
                new PropertyModel<String>(this, "state"), "input.jqueryid_state") {

            @Override
            public List<?> getMatches(String term) {
                List<State> statesLike = StatesDb.getStatesLike(term.toString());
                return statesLike;
            }
        });

    }
}
